package deibay.client.core;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.logging.Level;

import deibay.DeibayLogger;
import deibay.client.Controller;
import deibay.client.IClientConnection;
import deibay.client.IModel;
import deibay.messages.Message;
import deibay.messages.RequestLoginMsg;
import deibay.messages.RequestRegisterMsg;
import deibay.network.IClientCommunicator;

/**
 * @author  Joel Cordeiro
 */
public class Login{

	private String user;
	private String pass;	
	private String role;		
	private IClientConnection communicator;		
	
	public Login(IModel model, Controller controller, String user, String pass)
	{
		this.user = user;
		this.pass = pass;		
		communicator = new SocketClientCommunicator(this, model, controller);		
	}		


	public void initializeThreads() {
		Thread tCommunicator = new Thread((SocketClientCommunicator)communicator);
		tCommunicator.start();		
	}


	
	public void setRole(String role)
	{
		this.role = role;
	}
	
	
	public String getRole()
	{
		return this.role;
	}
	
	
	public String getPass() {
		return pass;
	}

	
	public String getUser() {
		return user;
	}


	public IClientConnection getCommunicator() {
		return communicator;
	}	
	
	public void sendRegister(String email, String address, String role)
	{
		try {
			Message msg = new RequestRegisterMsg(RequestRegisterMsg.class.getSimpleName(), this.user, this.pass,email, address, role);
			this.communicator.send(msg);
		} catch (IOException e) {
			DeibayLogger.getInstance().getLog().log(Level.SEVERE, "Erro ao enviar mensagem de registo.\n");			
		}	
	}
	
	public void sendLogin()
	{
		try {
			Message msg = new RequestLoginMsg(RequestLoginMsg.class.getSimpleName(), this.user,this.pass);			
			this.communicator.send(msg);
		} catch (IOException e) {
			DeibayLogger.getInstance().getLog().log(Level.SEVERE, "Erro ao enviar mensagem de login.\n");			
		}	
	}
}
